;
(function() {
	var role = require("./role");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var GoodsCon = require("../../core/goods/_con");
	var ShortCon = require("../../core/short/_con");
	var AchCon = require("../../core/ach/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");

	module.exports = {
		prefix: "/bus/guide",
		get: {
			"/config": function() {
				var bus = role.getBus(this);
				var result = bus.getGuideConfig();
				this.body = ResponObj("json", result);
			},
			"/list": function() {
				var bus = role.getBus(this);
				var result = bus.getGuideList();
				this.body = ResponObj("json", result);
			},
			"/url": function() {
				var query = this.request.query;
				var guide_id = query.guide_id;
				if (!guide_id) {
					throwCusError("参数错误")
				}
				var bus = role.getBus(this);
				var site_url = bus.getOneVisitableUrl();
				if (site_url) {
					//把推荐者也改成是这个人
					//因为现在业务逻辑是，推荐返利、分销返利、只能有一个受益者
					//算钱的时候根据这个受益者的身份（是否推销员）来使用不同的返利比
					this.body = ResponObj("json", (new ShortCon(site_url + "?gi=" + guide_id + "&rcid=" + guide_id)).toUrl());
				} else {
					throwCusError("该商家ID未注册域名")
				}
			}
		},
		post: {
			"/add_guide": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var user_phone = body.user_phone;
				if (!user_phone) {
					throwCusError("导购号码不可为空")
				}
				bus.addGuideByUserPhone(user_phone.trim());
				this.body = ResponObj("json", bus.getGuideList());
			}
		},
		put: {
			"/config": function() {
				var bus = role.getBus(this);
				console.log(typeof this.request, typeof this.request.body);
				var guide_config = this.request.body.guide_config;
				bus.updateGuideConfig(guide_config);
				this.body = ResponObj("json", bus.getGuideConfig());
			},
			"/state/shenqingduihuanzhong_to_shangjiayijingfukuan": function() {
				var bus = role.getBus(this);
				var user_id = this.request.body.user_id;
				if (!user_id) {
					throwCusError("参数错误")
				}
				var bus_user_bind = bus.state_guide_shenqingduihuanzhong_to_shangjiayijingfukuan(user_id);
				this.body = ResponObj("json", bus_user_bind);
			}
		},
		"delete": {
			"/remove_guide": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var guide_id = body.guide_id;
				if (!guide_id) {
					throwCusError("参数错误")
				}
				bus.removeGuide(guide_id);
				this.body = ResponObj("json", bus.getGuideList());
			}
		}
	}
}());